#include "doodleSprite.h"
#include "gamedata.h"

void DoodleSprite::jump()
{
	//if(velocityY() == 0 )
	//{
	    velocityY(-250);
	//}
}

void DoodleSprite::moveLeft()
{
	faceLeft();
	if(velocityX() > -100)
	{
		velocityX(velocityX() - 50);
	}
}
void DoodleSprite::moveRight()
{
	faceRight();
	if(velocityX() < 100)
	{
		velocityX(velocityX() + 50);
	}
}
void DoodleSprite::update(unsigned ticks) {
  int pic_width = Gamedata::getInstance()->getXmlInt("worldWidth");
  int pic_height = Gamedata::getInstance()->getXmlInt("worldHeight");
  float time = static_cast<float>(ticks) * 0.001;
  float a = 150;
  if(Y() < pic_height+1.0 && Y() > pic_height -1.0)
  {
	  a = 0;
  }
	velocityY( velocityY() + a * time);
  float incr = velocityY()*time + 0.5 * a * time*time ;
  Y( Y()+incr );
  float height = static_cast<float>(frame->getHeight());
  /*if ( Y() < 0) {
    velocityY( abs( velocityY() ) );
  }
  */
  if ( Y() > pic_height - height) {
    velocityY( 0 );
    Y(pic_height - height );
  }

	float f;
	if (velocityY()!=0)
	{
		f = 0;
	}
	else if(abs(velocityX())<0.001)
	{
		f = 0;
		velocityX(0);
	}
	else
		f = -velocityX()/abs(velocityX())*500;
	velocityX( velocityX() + f * time);
  float incrX = velocityX()*time + 0.5* f *time*time;
  X( X()+incrX );
  float width = static_cast<float>(frame->getWidth());
  if ( X() < 0) {
    velocityX( abs( velocityX() ) );
    X(0);
  }
  if ( X() >  pic_width-width) {
    velocityX( -abs( velocityX() ) );
    X(pic_width - width);
  }

  //set the highest position
  if (Y() < highestPos[1]) {
	  highestPos = getPosition();
  }
}
